<?php
require_once "includes/db.php";
require_once "includes/functions.inc.php";

$user_id = RetrieveUserIDFromHash();

if (!(isset($_POST['network']) && isset($_POST['data']) && isset($_POST['network_user_id'])))
    die("14"); // Missing POST arguments

$sql = "SELECT network_id, network_name FROM networks WHERE ";

if (is_numeric($_POST['network']))
    $sql .= "network_id = '" . $_POST['network'] . "';";
else
    $sql .= "network_name LIKE '" . mysql_real_escape_string($_POST['network']) . "';";

$result = mysql_query($sql);

if (!$result)
    die("6"); // technical error

if (mysql_num_rows($result) != 1) 
    die("6"); // technicut :3
	
$network_data = mysql_fetch_array($result);
var_dump($_POST['data']);

$data = json_decode($_POST['data'], true);

if ($data == NULL) 
    die("JSON string was malformed!");

// in dit geval vrienden toe gaan voegen vanuit de JSON
if ($network_data['network_name'] == "ADD_FRIENDS")
{
    // dit is de ADD_FRIENDS
    if (count($data)>0)
    {
        $pastFirst = false;
        $sql = "INSERT INTO `friends` (`friend_user_id_1`,`friend_user_id_2`) VALUES ";

        foreach ($data as $friend_id => $friend) 
        {
            if(is_numeric($friend['ID']))
            {
                if ($pastFirst) 
                {
                    $sql .= ", ";
                }

                $sql .= "('".$user_id."', '".$friend['ID']."')";

                $pastFirst = true;
            }
        }

        $sql .= ";";

        $result = mysql_query($sql);

        if (!$result) 
            die("6"); // technical SQL error

        echo "8"; // vrienden succesvol toegevoegd
    }
}
else // returns all users that were found as possible friends	
{
    $sql = "SELECT user_id FROM user_network_link WHERE network_user_id = '" . mysql_real_escape_string($_POST['network_user_id']) . "' LIMIT 1";
    $result = mysql_query($sql);

    if (!$result)
        die("6"); // technical SQL error

    if (mysql_num_rows($result) == 0) 
    {
        // OP DEZE KUTPLEK WORDT DE LINK GELEGD TUSSEN JE FACEBOOK ACCOUNT (OF JE TWITTER ACCOUNT) EN JE
        // BOBSHAKER ACCOUNT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!111111111111111111111111
        $sql = "INSERT INTO user_network_link (user_id, network_id, network_user_id) VALUES ('" . $split[0] . "', '" . $network_data['network_id'] . "', '" . mysql_real_escape_string($_POST['network_user_id']) . "')";

        if (!mysql_query($sql))
            die("2");

        $sql = "SELECT user_id FROM user_network_link WHERE network_user_id = '" . mysql_real_escape_string($_POST['network_user_id']) . "' LIMIT 1";

        $result = mysql_query($sql);

        if (!$result)
            die("2");

        if (mysql_num_rows($result) == 0)
            die("6"); // technical SQL error
    }

    $user = mysql_fetch_array($result);

    if ($user['user_id'] == $split[0]) 
    {
        $possible_contacts = array();

        switch ($network_data['network_name']) 
        {
            case "facebook":
            case "twitter":
                $sql = "SELECT `users`.`user_id`, `user_first_name`, `user_last_name` FROM `users`, `user_network_link` WHERE `network_id` = '" . $network_data['network_id'] . "' AND `user_network_link`.`user_id` = `users`.`user_id` AND `network_user_id` IN (";
                $i = 0;

            foreach ($data as $friend_id => $friend) 
            {
                if ($i != 0) 
                {
                    $sql .= ", ";
                }

                $sql .= "'" . $friend['id'] . "'";

                $i++;
            }

            $sql .= ");";

            $result = mysql_query($sql);

            if (!$result) 
                die("6"); // SQL error

            if (mysql_num_rows($result) == 0) 
            {
                //triest geen zekere vrienden gevonden
                $sql = "SELECT `user_id`, `user_first_name`, `user_last_name` FROM `users` WHERE ";
                $i = 0;

                foreach ($data as $friend_id => $friend) 
                {
                    if ($i != 0) 
                    {
                        $sql .= " OR ";
                    }

                    if ($network_data['network_name'] == "facebook") 
                    {
                        $sql .= "(`user_first_name` LIKE '" . mysql_real_escape_string($friend['first_name']) . "' AND `user_last_name` LIKE '" . mysql_real_escape_string($friend['last_name']) . "')";
                    } 
                    else 
                    {
                        $naam_delen = explode(" ", $friend['name']);

                        if (count($naam_delen) > 2) 
                        {
                                for ($i = 2; $i < count($naam_delen); $i++) 
                                {
                                        $naam_delen[1] .= " " . $naam_delen[$i];
                                }
                        }

                        $sql .= "(`user_first_name` LIKE '" . mysql_real_escape_string($naam_delen[0]) . "' AND `user_last_name` LIKE '" . mysql_real_escape_string($naam_delen[1]) . "') OR user_name = '" . mysql_real_escape_string($friend['screen_name']) . "'";
                    }
                    $i++;
                }
                $sql .= ";";

                //letop deze query kan best flink lang worden
                //but not to fear!!! max size limit is standaard 1 gb
                $result = mysql_query($sql);

                if (!$result) 
                    die("2"); // technische storing

                if (mysql_num_rows($result) == 0) 
                    die("9"); //HAHA GEEN VRIENDEN!! PWNT!!!
            }

            $possible_contacts = array();

            while ($row = mysql_fetch_array($result)) 
            {
                $possible_contacts[$row['user_id']] = $row;
                        //$possible_contacts = new FriendInfo(networkuserid, firstname, lastname, media)
                //$possible_contacts[] = new FriendInfo($row['user_id'], 
            }

            break;
        }

        // HIER DE MOGELIJKE VRIENDEN
        echo json_encode($possible_contacts);
    }
}
?>